import axios from 'axios';
import qs from 'qs';
import ServerMixin from '@/mixin/ServerMixin'

export const doExec = (url, params, contentType, methods) => {
    let pstr = qs.stringify(params, { arrayFormat: 'brackets' });
    return axios({
            url: (methods && methods.toUpperCase()) !== 'GET' ? url : `${url}?${pstr}`,
            params: null,
            data: (contentType && contentType.toUpperCase()) === 'JSON' ? params : qs.stringify(params),
            method: methods || 'POST'
        })
        .then((res) => {
            if (res.data.code == -2) {
                ServerMixin.Event.$emit('login');
            } else {
                return res.data;
            }
        });
};